package com.nbcio.modules.crm.activity.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nbcio.common.core.domain.R;
import com.nbcio.modules.crm.activity.domain.CrmActivity;
import com.nbcio.modules.crm.activity.service.ICrmActivityService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * CRM销售活动管理控制器
 */
@Tag(name = "CRM销售活动管理")
@RestController
@RequestMapping("/crm/activity")
@RequiredArgsConstructor
public class CrmActivityController {

    private final ICrmActivityService activityService;

    /**
     * 分页查询销售活动
     */
    @GetMapping("/page")
    public R<IPage<CrmActivity>> page(
            @RequestParam int pageNum,
            @RequestParam int pageSize,
            @RequestParam(required = false) String activityName,
            @RequestParam(required = false) String activityType,
            @RequestParam(required = false) Long customerId,
            @RequestParam(required = false) Long contactId,
            @RequestParam(required = false) Long opportunityId,
            @RequestParam(required = false) Long ownerUserId,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime) {
        LambdaQueryWrapper<CrmActivity> qw = new LambdaQueryWrapper<>();
        if (activityName != null && !activityName.isEmpty()) {
            qw.like(CrmActivity::getActivityName, activityName);
        }
        if (activityType != null && !activityType.isEmpty()) {
            qw.eq(CrmActivity::getActivityType, activityType);
        }
        if (customerId != null) {
            qw.eq(CrmActivity::getCustomerId, customerId);
        }
        if (contactId != null) {
            qw.eq(CrmActivity::getContactId, contactId);
        }
        if (opportunityId != null) {
            qw.eq(CrmActivity::getOpportunityId, opportunityId);
        }
        if (ownerUserId != null) {
            qw.eq(CrmActivity::getOwnerUserId, ownerUserId);
        }
        if (status != null && !status.isEmpty()) {
            qw.eq(CrmActivity::getStatus, status);
        }
        if (startTime != null && endTime != null && !startTime.isEmpty() && !endTime.isEmpty()) {
            qw.between(CrmActivity::getActivityDate, startTime, endTime);
        }
        qw.orderByDesc(CrmActivity::getActivityDate);
        IPage<CrmActivity> page = activityService.page(Page.of(pageNum, pageSize), qw);
        return R.ok(page);
    }

    /**
     * 根据ID查询销售活动
     */
    @GetMapping("/{id}")
    public R<CrmActivity> get(@PathVariable("id") String id) {
        CrmActivity activity = activityService.getById(Long.parseLong(id));
        return R.ok(activity);
    }

    /**
     * 新增销售活动
     */
    @PostMapping
    public R<Boolean> create(@RequestBody CrmActivity activity) {
        return R.ok(activityService.save(activity));
    }

    /**
     * 更新销售活动
     */
    @PutMapping("/{id}")
    public R<Boolean> update(@PathVariable("id") String id, @RequestBody CrmActivity activity) {
        activity.setActivityId(Long.parseLong(id));
        return R.ok(activityService.updateById(activity));
    }

    /**
     * 删除销售活动
     */
    @DeleteMapping("/{id}")
    public R<Boolean> remove(@PathVariable("id") String id) {
        return R.ok(activityService.removeById(Long.parseLong(id)));
    }

    /**
     * 批量删除销售活动
     */
    @PostMapping("/batch-delete")
    public R<Boolean> batchDelete(@RequestBody List<String> ids) {
        List<Long> longIds = ids.stream().map(Long::parseLong).collect(Collectors.toList());
        return R.ok(activityService.removeByIds(longIds));
    }
}


